﻿Imports Microsoft.Win32
Imports MyAddin_40.Common
Imports System.IO
Imports System.Reflection
Imports System.Runtime.InteropServices
Imports System.Windows.Forms

<GuidAttribute("D533FD34-A05C-4B54-8758-AFB2FECE0C4F"), ProgId("MyAwesomeAddin.Addin"), ComVisible(True)> _
<AddinTitle("MyAwesomeAddin"), AddinSummary("MyAwesomeAddin summary")>
<AddinEnvironmentCategory(SECATIDS.CATID_SEAllDocumentEnvrionments)>
Public Class Addin
    Implements SolidEdgeFramework.ISolidEdgeAddIn
    Implements SolidEdgeFramework.ISEAddInEvents
    Implements SolidEdgeFramework.ISEApplicationEvents

    Private _application As SolidEdgeFramework.Application
    Private _addinEx As SolidEdgeFramework.ISEAddInEx
    Private _connectionPoints As Dictionary(Of System.Runtime.InteropServices.ComTypes.IConnectionPoint, Int32)
    Private _edgeBarManager As EdgeBarManager

    Public Sub OnConnection(Application As Object, ConnectMode As SolidEdgeFramework.SeConnectMode, AddInInstance As SolidEdgeFramework.AddIn) Implements SolidEdgeFramework.ISolidEdgeAddIn.OnConnection
        _application = Application
        _addinEx = AddInInstance

        If _addinEx Is Nothing Then
            MessageBox.Show("This version of Solid Edge is too old.")
            Throw New System.NotImplementedException()
        End If

        _edgeBarManager = New EdgeBarManager()

        _addinEx.GuiVersion = 1
        _addinEx.Visible = True

        _connectionPoints = New Dictionary(Of System.Runtime.InteropServices.ComTypes.IConnectionPoint, Int32)

        HookEvents(_application, GetType(SolidEdgeFramework.ISEApplicationEvents).GUID)
        HookEvents(_addinEx, GetType(SolidEdgeFramework.ISEAddInEvents).GUID)

    End Sub

    Public Sub OnConnectToEnvironment(EnvCatID As String, pEnvironmentDispatch As Object, bFirstTime As Boolean) Implements SolidEdgeFramework.ISolidEdgeAddIn.OnConnectToEnvironment
        Dim strResPath As String
        Dim strCategoryName As String
        Dim commandIDs As Array
        Dim strCommandNames As Array
        Dim environmentGuid As Guid

        environmentGuid = New Guid(EnvCatID)

        strResPath = Path.ChangeExtension(Assembly.GetExecutingAssembly().Location, "Resources.dll")
        strCategoryName = "My Category"

        If environmentGuid.Equals(New Guid(SECATIDS.CATID_SEPart)) Then
            commandIDs = Array.CreateInstance(GetType(System.Int32), 2)
            strCommandNames = Array.CreateInstance(GetType(System.String), 2)

            commandIDs.SetValue(1, 0)
            commandIDs.SetValue(2, 1)

            strCommandNames.SetValue("MyAwesomeAddin command 1" & Chr(10) & "MyAwesomeAddin Command1" & Chr(10) & "Sample 1" & Chr(10) & "Sample command 1", 0)
            strCommandNames.SetValue("Don't click this button!" & Chr(10) & "Don't click this button!" & Chr(10) & "Don't click this button!" & Chr(10) & "Don't click this button!", 1)

            _addinEx.SetAddInInfoEx(strResPath, EnvCatID, strCategoryName, 101, 102, -1, -1, commandIDs.Length, strCommandNames, commandIDs)

        End If

    End Sub

    Public Sub OnDisconnection(DisconnectMode As SolidEdgeFramework.SeDisconnectMode) Implements SolidEdgeFramework.ISolidEdgeAddIn.OnDisconnection
        UnhookEvents()
        Marshal.ReleaseComObject(_addinEx)
        Marshal.ReleaseComObject(_application)
    End Sub

    <ComRegisterFunctionAttribute()> _
    Public Shared Sub RegisterFunction(ByVal t As Type)
        Registration.CommonRegisterFunction(t)
    End Sub

    <ComUnregisterFunctionAttribute()> _
    Public Shared Sub UnregisterFunction(ByVal t As Type)
        Registration.CommonUnregisterFunction(t)
    End Sub

    Private Sub HookEvents(source As Object, eventGuid As Guid)
        Dim cpc As System.Runtime.InteropServices.ComTypes.IConnectionPointContainer = Nothing
        Dim cp As System.Runtime.InteropServices.ComTypes.IConnectionPoint = Nothing
        Dim cookie As Int32

        Try
            cpc = source
            If Not cpc Is Nothing Then
                cpc.FindConnectionPoint(eventGuid, cp)
                If Not cp Is Nothing Then
                    cp.Advise(Me, cookie)
                    If cookie <> 0 Then
                        _connectionPoints.Add(cp, cookie)
                    Else
                        Throw New System.Exception("Advisory connection between the connection point and the caller's sink object failed.")
                    End If
                Else
                    Throw New System.Exception("Unable to find connection point.")
                End If
            Else
                Throw New System.Exception("Source does not implement IConnectionPointContainer.")
            End If
        Catch ex As Exception
            Throw
        End Try
    End Sub

    Private Sub UnhookEvents()
        Dim enumerator As Dictionary(Of System.Runtime.InteropServices.ComTypes.IConnectionPoint, Int32).Enumerator

        enumerator = _connectionPoints.GetEnumerator()

        While enumerator.MoveNext()
            Try
                enumerator.Current.Key.Unadvise(enumerator.Current.Value)
            Catch ex As Exception

            End Try
        End While
    End Sub

#Region "SolidEdgeFramework.ISEAddInEvents implementation"


    Public Sub OnCommand(CommandID As Integer) Implements SolidEdgeFramework.ISEAddInEvents.OnCommand
        Dim objEnvironments As SolidEdgeFramework.Environments
        Dim objEnvironment As SolidEdgeFramework.Environment
        Dim environmentGuid As Guid

        objEnvironments = _application.Environments
        objEnvironment = objEnvironments.Item(_application.ActiveEnvironment)

        environmentGuid = New Guid(objEnvironment.CATID)

        If environmentGuid.Equals(New Guid(SECATIDS.CATID_SEPart)) Then
            Select Case CommandID
                Case 1
                    MsgBox("MyAwesomeAddin command 1")
                Case 2
                    Marshal.FinalReleaseComObject(_application)
            End Select
        End If
    End Sub

    Public Sub OnCommandHelp(hFrameWnd As Integer, HelpCommandID As Integer, CommandID As Integer) Implements SolidEdgeFramework.ISEAddInEvents.OnCommandHelp

    End Sub

    Public Sub OnCommandUpdateUI(CommandID As Integer, ByRef CommandFlags As Integer, ByRef MenuItemText As String, ByRef BitmapID As Integer) Implements SolidEdgeFramework.ISEAddInEvents.OnCommandUpdateUI

    End Sub

#End Region

#Region "SolidEdgeFramework.ISEApplicationEvents implementation"


    Public Sub AfterActiveDocumentChange(theDocument As Object) Implements SolidEdgeFramework.ISEApplicationEvents.AfterActiveDocumentChange

    End Sub

    Public Sub AfterCommandRun(theCommandID As Integer) Implements SolidEdgeFramework.ISEApplicationEvents.AfterCommandRun

    End Sub

    Public Sub AfterDocumentOpen(theDocument As Object) Implements SolidEdgeFramework.ISEApplicationEvents.AfterDocumentOpen

    End Sub

    Public Sub AfterDocumentPrint(theDocument As Object, hDC As Integer, ByRef ModelToDC As Double, ByRef Rect As Integer) Implements SolidEdgeFramework.ISEApplicationEvents.AfterDocumentPrint

    End Sub

    Public Sub AfterDocumentSave(theDocument As Object) Implements SolidEdgeFramework.ISEApplicationEvents.AfterDocumentSave

    End Sub

    Public Sub AfterEnvironmentActivate(theEnvironment As Object) Implements SolidEdgeFramework.ISEApplicationEvents.AfterEnvironmentActivate

    End Sub

    Public Sub AfterNewDocumentOpen(theDocument As Object) Implements SolidEdgeFramework.ISEApplicationEvents.AfterNewDocumentOpen
        _edgeBarManager.AddPage(_addinEx, theDocument)
    End Sub

    Public Sub AfterNewWindow(theWindow As Object) Implements SolidEdgeFramework.ISEApplicationEvents.AfterNewWindow

    End Sub

    Public Sub AfterWindowActivate(theWindow As Object) Implements SolidEdgeFramework.ISEApplicationEvents.AfterWindowActivate

    End Sub

    Public Sub BeforeCommandRun(theCommandID As Integer) Implements SolidEdgeFramework.ISEApplicationEvents.BeforeCommandRun

    End Sub

    Public Sub BeforeDocumentClose(theDocument As Object) Implements SolidEdgeFramework.ISEApplicationEvents.BeforeDocumentClose
        _edgeBarManager.RemovePage(theDocument)
    End Sub

    Public Sub BeforeDocumentPrint(theDocument As Object, hDC As Integer, ByRef ModelToDC As Double, ByRef Rect As Integer) Implements SolidEdgeFramework.ISEApplicationEvents.BeforeDocumentPrint

    End Sub

    Public Sub BeforeDocumentSave(theDocument As Object) Implements SolidEdgeFramework.ISEApplicationEvents.BeforeDocumentSave

    End Sub

    Public Sub BeforeEnvironmentDeactivate(theEnvironment As Object) Implements SolidEdgeFramework.ISEApplicationEvents.BeforeEnvironmentDeactivate

    End Sub

    Public Sub BeforeQuit() Implements SolidEdgeFramework.ISEApplicationEvents.BeforeQuit
        _edgeBarManager.RemoveAllPages()
    End Sub

    Public Sub BeforeWindowDeactivate(theWindow As Object) Implements SolidEdgeFramework.ISEApplicationEvents.BeforeWindowDeactivate

    End Sub

#End Region

End Class
